package com.ambercrm.common;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ambercrm.R;


public class StateLayout extends RelativeLayout {
    Context mContext;
    public StateLayout(Context context) {
        super(context);
        init(context);
    }

    public StateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public StateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    LayoutInflater mInflater;
    View mView;

    // 加载页面
    RelativeLayout mLoaddingLayout;
    ImageView mLoadingImgV;
    TextView mLoaddingTextV;
    // 空页面
    RelativeLayout mEmptyLayout;
    ImageView mEmptyImgV;
    TextView mEmptyTextV;
    // 系统异常
    RelativeLayout mSystemErrorLayout;
    ImageView mSystemErrorImgV;
    TextView mSystemErrorTextV;
    Button mSystemErrorTryAgainBt;
    // 网络异常
    RelativeLayout mNetworkErrorLayout;
    Button mNetworkTryAgainBt;
    private void init(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mView = mInflater.inflate(R.layout.empty_error_syserr_layout, null);
        addView(mView);
        // 加载页面控件
        mLoaddingLayout = mView.findViewById(R.id.loadding_layout);
        mLoadingImgV = mView.findViewById(R.id.loading_img);
        mLoaddingTextV = mView.findViewById(R.id.loading_text);

        // 空页面
        mEmptyLayout = mView.findViewById(R.id.empty_layout);
        mEmptyImgV = mView.findViewById(R.id.empty_imageview);
        mEmptyTextV = mView.findViewById(R.id.empty_textview);

        // 系统异常页面
        mSystemErrorLayout = mView.findViewById(R.id.sytem_error_layout);
        mSystemErrorImgV = mView.findViewById(R.id.system_error_imageview);
        mSystemErrorTextV = mView.findViewById(R.id.system_error_textview);
        mSystemErrorTryAgainBt = mView.findViewById(R.id.sys_try_again);

        // 网络异常
        mNetworkErrorLayout = mView.findViewById(R.id.network_error_layout);
        mNetworkTryAgainBt = mView.findViewById(R.id.network_try_again);
    }

    // 显示加载页面
    public void showLoaddingLayout(String text) {
        mLoaddingLayout.setVisibility(View.VISIBLE);
        mLoaddingTextV.setText(text);
        mEmptyLayout.setVisibility(View.GONE);
        mSystemErrorLayout.setVisibility(View.GONE);
        mNetworkErrorLayout.setVisibility(View.GONE);

        Animation circle_anim = AnimationUtils.loadAnimation(getContext(), R.anim.anim_round_rotate);
        //设置匀速旋转，在xml文件中设置会出现卡顿
        LinearInterpolator interpolator = new LinearInterpolator();
        circle_anim.setInterpolator(interpolator);
        mLoadingImgV.startAnimation(circle_anim);
    }

    // 显示空页面
    public void showEmptyLayout(String text, int imgResId) {
        mLoaddingLayout.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.VISIBLE);
        mEmptyTextV.setText(text);
        mEmptyImgV.setImageResource(imgResId);
        mSystemErrorLayout.setVisibility(View.GONE);
        mNetworkErrorLayout.setVisibility(View.GONE);
    }

    // 显示系统错误页面
    public void showSystemErrorLayout(String text, int imgResId, OnClickListener onClickListener) {
        mLoaddingLayout.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.GONE);
        mSystemErrorLayout.setVisibility(View.VISIBLE);
        mSystemErrorImgV.setImageResource(imgResId);
        mSystemErrorTextV.setText(text);
        mSystemErrorTryAgainBt.setOnClickListener(onClickListener);
        mNetworkErrorLayout.setVisibility(View.GONE);
    }

    // 显示网络错误页面
    public void showNetworkErrorLayout(OnClickListener onClickListener) {
        mLoaddingLayout.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.GONE);
        mSystemErrorLayout.setVisibility(View.GONE);
        mNetworkErrorLayout.setVisibility(View.VISIBLE);
        mNetworkTryAgainBt.setOnClickListener(onClickListener);
    }

    // 显示内容页面
    public void showContentLayout() {
        mLoaddingLayout.setVisibility(View.GONE);
        mEmptyLayout.setVisibility(View.GONE);
        mSystemErrorLayout.setVisibility(View.GONE);
        mNetworkErrorLayout.setVisibility(View.GONE);
    }

    // 隐藏加载页面
    public void hideLoaddingLayout() {
        mLoaddingLayout.setVisibility(View.GONE);
    }

    // 隐藏空页面
    public void hideEmptyLayout() {
        mEmptyLayout.setVisibility(View.GONE);
    }

    // 隐藏系统异常页面
    public void hideSystemErrorLayout() {
        mSystemErrorLayout.setVisibility(View.GONE);
    }

    // 隐藏网络错误页面
    public void hideNetworkErrorLayout() {
        mNetworkErrorLayout.setVisibility(View.GONE);
    }
}
